// 云函数入口文件
const cloud = require('wx-server-sdk')
// 用户信息写入
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
  })
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const openid = wxContext.OPENID;


    const count1 = await db.collection("userInfo").where({
        openid:openid,
        status:1
    }).count();

    if(count1.total){
      return "dark"
    }

    const count = await db.collection("userInfo").where({
        openid:openid,
        status:0
    }).count();

    if(count.total){
      db.collection("userInfo").where({
        openid
    }).update({
        nickName:event.nickName,
        avatarUrl:event.avatarUrl
    })
    
    }else{
        // 如果没有数据进行增加
        db.collection("userInfo").add({
            data:{
                nickName:event.nickName,
                avatarUrl:event.avatarUrl,
                openid:openid,
                status:0
            }
        })
    }

    return openid
}